package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfMealTime;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfMealTime;
import com.ocom.payhandle.service.XfMealTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfMealTimeServiceImpl extends ServiceImpl<IDaoXfMealTime, EntityXfMealTime> implements XfMealTimeService {

    @Autowired
    private IDaoXfMealTime daoXfMealTime;


    //2.2.获取餐段时间信息(getMealTimeInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityXfMealTime> GetMealTimeInfo(Long comId) {
        //select meal_no , begin_time , end_time from xf_meal_time
        //				where com_id = companyID and point_id = 0 and acc_type_no = 0
        //				order by meal_no

        QueryWrapper<EntityXfMealTime> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("point_id",0);
        query.eq("acc_type_id",0);
        query.orderByAsc("meal_no");

        return this.list(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public List<EntityXfMealTime> selectTimeList(Long companyID,Long pointId) {
        if (pointId != null && pointId !=0l){
            QueryWrapper<EntityXfMealTime> query = Wrappers.query();
            query.eq("com_id", companyID);
            query.eq("point_id", pointId);
            query.eq("acc_type_id", 0);
            query.orderByAsc("meal_no");
            List<EntityXfMealTime> mealTimeList = this.list(query);
            if (mealTimeList != null && mealTimeList.size() > 0) {
                return mealTimeList;
            }
        }
        QueryWrapper<EntityXfMealTime> query2 = Wrappers.query();
        query2.eq("com_id",companyID);
        query2.eq("point_id",0);
        query2.eq("acc_type_id",0);
        query2.orderByAsc("meal_no");
        return this.list(query2);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public List<EntityXfMealTime> selectTimeList2(Long companyID,Long pointId) {
        QueryWrapper<EntityXfMealTime> query = Wrappers.query();
        query.eq("com_id",companyID);
        query.eq("point_id",pointId);
//        query.eq("acc_type_id",0);
        query.orderByAsc("meal_no");
        return this.list(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public List<EntityXfMealTime> getMealInfo(Long comId) {
        return daoXfMealTime.getMealInfo(comId);
    }



    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public List<EntityXfMealTime> selectTimeList3(Long companyId, Long pointId, Integer accTypeId) {
        if (pointId == null) {
            pointId = 0l;
        }
        if (accTypeId == null) {
            accTypeId = 0;
        }
        QueryWrapper<EntityXfMealTime> query = Wrappers.query();
        query.eq("com_id", companyId);
        query.eq("point_id", pointId);
        query.eq("acc_type_id", accTypeId);
        query.orderByAsc("meal_no");
        List<EntityXfMealTime> mealTimeList = this.list(query);
        if (mealTimeList.size() > 0) {
            return mealTimeList;
        }

        QueryWrapper<EntityXfMealTime> query1 = Wrappers.query();
        query1.eq("com_id", companyId);
        query1.eq("point_id", pointId);
        query1.eq("acc_type_id", 0);
        query1.orderByAsc("meal_no");
        mealTimeList = this.list(query1);
        if (mealTimeList.size() > 0) {
            return mealTimeList;
        }

        QueryWrapper<EntityXfMealTime> query2 = Wrappers.query();
        query2.eq("com_id", companyId);
        query2.eq("point_id", 0);
        query2.eq("acc_type_id", 0);
        query2.orderByAsc("meal_no");
        return this.list(query2);
    }

}
